package com.augurit.gzcgwsp.sendNotice.service.impl;

import java.io.StringReader;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.hibernate.SQLQuery;
import org.hibernate.transaction.TransactionFactory;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xml.sax.InputSource;

import com.augurit.common.syslog.tool.SysTraceLogWriter;
import com.augurit.common.system.service.org.IOmOrgService;
import com.augurit.common.system.service.org.IOmUserService;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.gzcgwsp.commonex.util.lang.StringUtilsEX;
import com.augurit.gzcgwsp.sendNotice.entity.YwWebserviceInfo;
import com.augurit.gzcgwsp.sendNotice.service.IUserForFxService;
import com.augurit.gzcgwsp.sendNotice.service.IYwWebserviceInfoService;
import com.augurit.gzcgwsp.smartformCsdlwj.common.SmartDao;

/**
 * 用户 行政组织 同步
 * 
 * @author adm
 * 
 */
@Service
@Transactional
public class UserForFxServiceImpl implements IUserForFxService {
	Logger logger = (Logger) LoggerFactory.getLogger(UserForFxServiceImpl.class);
	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale);
	// 用户 行政组织 同步url
	private final String exchangeuserurl = localResource.getString("exchangeuserurl");
	
	@Autowired
	private IYwWebserviceInfoService ywWebserviceInfoService;
	@Autowired
	private IOmUserService omUserService;
	@Autowired
	private SmartDao smartDao;
	@Autowired
	private IOmOrgService omOrgService;
	/**
	 * 审批系统新增行政组织
	 * @param form
	 */
	public void saveOrganization(OmOrgForm form){
		Document document = DocumentHelper.createDocument();
		//生成一个接点
		Element info = document.addElement("info");
		info.addElement("orgCode").addText(form.getOrgId().toString());
		info.addElement("orgName").addText(form.getOrgName());
		//获得父节点form
		OmOrgForm parentForm = omOrgService.get(form.getParentOrgId());
		info.addElement("parentOrgCode").addText(parentForm.getOrgId().toString());
		info.addElement("levelCode").addText("");
		info.addElement("sysCode").addText("000003");
		info.addElement("orgDesc").addText(form.getRemark());
		info.addElement("status").addText("");
		info.addElement("updateTime").addText("");
		String sb = document.asXML();
		try {
			
			String para = StringUtilsEX.base64Encode(sb,"UTF-8");
			YwWebserviceInfo ywWebserviceInfo = new YwWebserviceInfo();
			ywWebserviceInfo.setParaTwo("TYYH_UserSynchronize_Service");
			ywWebserviceInfo.setParaThree("newOrganization");
			ywWebserviceInfo.setWsdlPara(exchangeuserurl);
//			ywWebserviceInfo.setFilePath(para);
			ywWebserviceInfo.setXmlStr(para);
			ywWebserviceInfo.setProduceDate(new Date());
			ywWebserviceInfoService.save(ywWebserviceInfo);	
		} catch (Exception e) {
			logger.error("审批系统新增行政组织接口保存异常"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			System.out.println(e.getMessage());
		}
	}
	
	/**
	 * 审批系统修改行政组织
	 * @param form
	 */
	public void updateOrganization(OmOrgForm form){
		Document document = DocumentHelper.createDocument();
		//生成一个接点
		Element info = document.addElement("info");
		info.addElement("orgCode").addText(form.getOrgId().toString());
		info.addElement("levelCode").addText("");
		//获得父节点form
		OmOrgForm parentForm = omOrgService.get(form.getParentOrgId());
		info.addElement("parentOrgCode").addText(parentForm.getOrgId().toString());
		info.addElement("orgName").addText(form.getOrgName());
		info.addElement("sysCode").addText("000003");
		info.addElement("orgDesc").addText(form.getRemark());
		info.addElement("status").addText("");
		
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		info.addElement("updateTime").addText(sf.format(date));
		String sb = document.asXML();
		try {
			String para = StringUtilsEX.base64Encode(sb,"UTF-8");
			YwWebserviceInfo ywWebserviceInfo = new YwWebserviceInfo();
			ywWebserviceInfo.setParaTwo("TYYH_UserSynchronize_Service");
			ywWebserviceInfo.setParaThree("modifyOrganization");
			ywWebserviceInfo.setWsdlPara(exchangeuserurl);
//			ywWebserviceInfo.setFilePath(para);
			ywWebserviceInfo.setXmlStr(para);
			ywWebserviceInfo.setProduceDate(date);
			ywWebserviceInfoService.save(ywWebserviceInfo);	
		} catch (Exception e) {
			logger.error("审批系统修改行政组织接口保存异常"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			System.out.println(e.getMessage());
		}
	}
	
	/**
	 * 审批系统删除行政组织
	 * @param orgId
	 */
	public void deleteOrganization(Long orgId){
		Document document = DocumentHelper.createDocument();
		//生成一个接点
		Element info = document.addElement("info");
		info.addElement("orgCode").addText(orgId.toString());
		info.addElement("sysCode").addText("000003");
		String sb = document.asXML();
		try {
			String para = StringUtilsEX.base64Encode(sb,"UTF-8");
			YwWebserviceInfo ywWebserviceInfo = new YwWebserviceInfo();
			ywWebserviceInfo.setParaTwo("TYYH_UserSynchronize_Service");
			ywWebserviceInfo.setParaThree("disUseOrganization");
			ywWebserviceInfo.setWsdlPara(exchangeuserurl);
//			ywWebserviceInfo.setFilePath(para);
			ywWebserviceInfo.setXmlStr(para);
			ywWebserviceInfo.setProduceDate(new Date());
			ywWebserviceInfoService.save(ywWebserviceInfo);	
		} catch (Exception e) {
			logger.error("审批系统删除行政组织接口保存异常"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			System.out.println(e.getMessage());
		}
	}
	
	/**
	 * 审批系统用户变更归属行政组织
	 * @param userId
	 * @param oldOrgId
	 * @param targetOrgId
	 */
	public void moveUserToOrg(Long userId,Long oldOrgId,Long targetOrgId){
		OmUserForm form = omUserService.get(userId);
		Document document = DocumentHelper.createDocument();
		// 生成一个接点
		Element info = document.addElement("info");
		info.addElement("userCode").addText(form.getUserCode());
		info.addElement("sysCode").addText("000003");
		info.addElement("oldOrgCode").addText(oldOrgId.toString());
		info.addElement("newOrgCode").addText(targetOrgId.toString());
		String sb = document.asXML();
		try {
			String para = StringUtilsEX.base64Encode(sb,"UTF-8");
			YwWebserviceInfo ywWebserviceInfo = new YwWebserviceInfo();
			ywWebserviceInfo.setParaTwo("TYYH_UserSynchronize_Service");
			ywWebserviceInfo.setParaThree("modifyUserOrg");
			ywWebserviceInfo.setWsdlPara(exchangeuserurl);
//			ywWebserviceInfo.setFilePath(para);
			ywWebserviceInfo.setXmlStr(para);
			ywWebserviceInfo.setProduceDate(new Date());
			ywWebserviceInfoService.save(ywWebserviceInfo);
		} catch (Exception e) {
			logger.error("用户变更归属行政组织接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			System.out.println(e.getMessage());
		}
	}
	
	/**
	 * 方欣新增行政组织，调用新增保存
	 * 
	 * @param xmlStr
	 */
	public String newOrganization(String xmlStr){
		try {
			Map<String,String> map = xmlToMap(xmlStr);		
			OmOrgForm form = new OmOrgForm();
			form.setOrgCode(map.get("orgCode"));
			form.setOrgLevel(StringUtils.isNotBlank(map.get("levelCode"))?Integer.parseInt((map.get("levelCode"))):null);
			form.setParentOrgId(StringUtils.isNotBlank(map.get("parentOrgCode"))?Long.parseLong((map.get("parentOrgCode"))):null);
			form.setOrgName(map.get("orgName"));
			form.setRemark(map.get("orgDesc"));
//			form.setStatus(map.get("status"));
//			String updateTimeStr = map.get("updateTime");
//			if(StringUtils.isNotBlank(updateTimeStr)){
//				form.setUpdateTime(DateUtils.stringToDate(updateTimeStr,DateUtils.YYYY_MM_DD_HH_MM_SS));
//			}
			omOrgService.save(form);
			return resultXml("0", form.getOrgId().toString());
//		<sysCode>系统编号</sysCode>
//		<status>组织状态</status>
		} catch (Exception e) {
			logger.error("方欣新增行政组织保存接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR+ e.getMessage(), e);
			System.out.println(e.getMessage());
			return resultXml("1", e.getMessage());
		}
	}
	
	/**
	 * 方欣修改行政组织，调用修改保存
	 * 
	 * @param xmlStr
	 */
	public String modifyOrganization(String xmlStr){

		try {
			Map<String,String> map = xmlToMap(xmlStr);		
			OmOrgForm form = new OmOrgForm();
			form.setOrgId(Long.parseLong(map.get("orgCode")));
			form.setOrgLevel(StringUtils.isNotBlank(map.get("levelCode"))?Integer.parseInt((map.get("levelCode"))):null);
			form.setParentOrgId(StringUtils.isNotBlank(map.get("parentOrgCode"))?Long.parseLong((map.get("parentOrgCode"))):null);
			form.setOrgName(map.get("orgName"));
			form.setRemark(map.get("orgDesc"));
//			form.setStatus(map.get("status"));
//			String updateTimeStr = map.get("updateTime");
//			if(StringUtils.isNotBlank(updateTimeStr)){
//				form.setUpdateTime(DateUtils.stringToDate(updateTimeStr,DateUtils.YYYY_MM_DD_HH_MM_SS));
//			}
			omOrgService.save(form);
			return resultXml("0", form.getOrgId().toString());
//		<sysCode>系统编号</sysCode>
//		<status>组织状态</status>
		} catch (Exception e) {
			logger.error("方欣修改行政组织保存接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR+ e.getMessage(), e);
			System.out.println(e.getMessage());
			return resultXml("1", e.getMessage());
		}
	
	}
	
	/**
	 * 方欣删除行政组织，调用删除
	 * @param orgCode
	 * @return
	 */
	public String disUseOrganization(String xmlStr){
		Map<String,String> map = xmlToMap(xmlStr);		
		String orgCode = map.get("orgCode");
		try {
			if(StringUtils.isNotBlank(orgCode)){
				Long[] orgIds = new Long[1];
				orgIds[0] = Long.parseLong(orgCode);
				omOrgService.deleteOrgCascadeForFx(orgIds);
				return resultXml("0", "成功");
			}
		} catch (Exception e) {
			logger.error("方欣删除行政组织接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR+ e.getMessage(), e);
			System.out.println(e.getMessage());
			return resultXml("1", e.getMessage());
		}
		return resultXml("1", "行政组织orgCode："+orgCode+" 不存在");
	}
	
	/**
	 * 方欣用户变更归属行政组织
	 * @param xmlStr
	 * @return
	 */
	public String modifyUserOrg(String xmlStr){
		try {
			Map<String,String> map = xmlToMap(xmlStr);		
			String loginName = map.get("userCode");
			String sysCode = map.get("sysCode");
			String oldOrgCode = map.get("oldOrgCode");
			String newOrgCode = map.get("newOrgCode");		
			if(StringUtils.isNotBlank(loginName) && StringUtils.isNotBlank(oldOrgCode) && StringUtils.isNotBlank(newOrgCode)){
				OmUserForm form = omUserService.get(loginName);
				Long userId = form.getUserId();//查出userId
				omOrgService.moveUserToOrg(userId, Long.valueOf(oldOrgCode), Long.valueOf(newOrgCode));
				return resultXml("0", "成功");
			}
		} catch (Exception e) {
			logger.error("方欣用户变更归属行政组织接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR+ e.getMessage(), e);
			System.out.println(e.getMessage());
			return resultXml("1", e.getMessage());
		}
		return resultXml("1", "用户变更归属行政组织失败");
	}


	/**
	 * 审批系统新增用户
	 * 
	 * @param form
	 * @param orgId
	 * @param posId
	 */
	public void saveUser(OmUserForm form, Long orgId, Long posId) {
		try {
			Document document = DocumentHelper.createDocument();
			// 生成一个接点
			Element info = document.addElement("info");
			info.addElement("userCode").addText(form.getUserCode());
			Element orgCode = info.addElement("orgCode");
			// 生成orgCode里面的值
			if (orgId != null) {
				orgCode.addText(orgId.toString());
			} else {
				orgCode.addText(posId.toString());
			}
			info.addElement("userName").addText(form.getUserName());
			info.addElement("encryptPassword").addText(form.getPassword());
			info.addElement("position").addText(form.getUserCode());
			info.addElement("sex").addText("M".equals(form.getSex())?"1":"0");
			info.addElement("telephone").addText(form.getOfficeTel());
			info.addElement("mobilePhone").addText(form.getMobile());
			info.addElement("email").addText(form.getEmail());
			info.addElement("status").addText("1".equals(form.getIsActive()) ? "Y":"N");
			info.addElement("sysCode").addText("000003");			
			Date date = new Date();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			info.addElement("createTime").addText(sf.format(date));
			String sb = document.asXML();	
			String para = StringUtilsEX.base64Encode(sb,"UTF-8");
			YwWebserviceInfo ywWebserviceInfo = new YwWebserviceInfo();
			ywWebserviceInfo.setParaTwo("TYYH_UserSynchronize_Service");
			ywWebserviceInfo.setParaThree("newUser");
			ywWebserviceInfo.setWsdlPara(exchangeuserurl);
//			ywWebserviceInfo.setFilePath(para);
			ywWebserviceInfo.setXmlStr(para);
			ywWebserviceInfo.setProduceDate(new Date());
			ywWebserviceInfoService.save(ywWebserviceInfo);
		} catch (Exception e) {
			logger.error("审批系统新增用户接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			System.out.println(e.getMessage());
		}
	}

	/**
	 * 审批系统修改用户
	 * 
	 * @param form
	 * @param orgId
	 * @param posId
	 */
	public void updateUser(OmUserForm form, Long orgId, Long posId) {
		try {
			Document document = DocumentHelper.createDocument();
			// 生成一个接点
			Element info = document.addElement("info");
			info.addElement("userCode").addText(form.getUserCode());
			info.addElement("orgCode");
			info.addElement("userName").addText(form.getUserName());
			info.addElement("encryptPassword").addText(form.getPassword());
			info.addElement("position").addText(form.getUserCode());
			info.addElement("sex").addText(form.getSex() =="M"?"1":"0");
			info.addElement("telephone").addText(form.getOfficeTel());
			info.addElement("mobilePhone").addText(form.getMobile());
			info.addElement("email").addText(form.getEmail());
			info.addElement("status").addText(form.getIsActive() == "1" ? "Y":"N");
			info.addElement("sysCode").addText("000003");
			
			Date date = new Date();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			info.addElement("createTime").addText(sf.format(date));
			String sb = document.asXML();
			String para = StringUtilsEX.base64Encode(sb,"UTF-8");
			YwWebserviceInfo ywWebserviceInfo = new YwWebserviceInfo();
			ywWebserviceInfo.setParaTwo("TYYH_UserSynchronize_Service");
			ywWebserviceInfo.setParaThree("modifyUser");
			ywWebserviceInfo.setWsdlPara(exchangeuserurl);
//			ywWebserviceInfo.setFilePath(para);
			ywWebserviceInfo.setXmlStr(para);
			ywWebserviceInfo.setProduceDate(new Date());
			ywWebserviceInfoService.save(ywWebserviceInfo);
		} catch (Exception e) {
			logger.error("审批系统修改用户接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			System.out.println(e.getMessage());
		}
	}

	/**
	 * 审批系统更新用户密码
	 */
	public void updateUserPassword(String userCode,String oldPassword,String newPassword) {
		try {
			Document document=DocumentHelper.createDocument();
			Element root=document.addElement("info");
			root.addElement("userCode").addText(userCode);
			root.addElement("oldPassword").addText(oldPassword);
			root.addElement("newPassword").addText(newPassword);
			String sb = document.asXML();
			String para = StringUtilsEX.base64Encode(sb,"UTF-8");
			YwWebserviceInfo ywWebserviceInfo = new YwWebserviceInfo();
			ywWebserviceInfo.setParaTwo("TYYH_UserSynchronize_Service");
			ywWebserviceInfo.setParaThree("modifyUserPassword");
			ywWebserviceInfo.setWsdlPara(exchangeuserurl);
//			ywWebserviceInfo.setFilePath(para);
			ywWebserviceInfo.setXmlStr(para);
			ywWebserviceInfo.setProduceDate(new Date());
			ywWebserviceInfoService.save(ywWebserviceInfo);
		} catch (Exception e) {
			logger.error("审批系统更新用户密码接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			System.out.println(e.getMessage());
		}
	}

	/**
	 * 审批系统删除用户
	 * 
	 * @param userId
	 */
	public void deleteUser(Long userId) {
		try {
			Document document = DocumentHelper.createDocument();
			// 生成一个接点
			Element info = document.addElement("info");		
			OmUserForm form = omUserService.get(userId);
			info.addElement("userCode").addText(form.getUserCode());
			info.addElement("sysCode").addText("000003");
			String sb = document.asXML();
			String para = StringUtilsEX.base64Encode(sb,"UTF-8");
			YwWebserviceInfo ywWebserviceInfo = new YwWebserviceInfo();
			ywWebserviceInfo.setParaTwo("TYYH_UserSynchronize_Service");
			ywWebserviceInfo.setParaThree("disuseUser");
			ywWebserviceInfo.setWsdlPara(exchangeuserurl);
//			ywWebserviceInfo.setFilePath(para);
			ywWebserviceInfo.setXmlStr(para);
			ywWebserviceInfo.setProduceDate(new Date());
			ywWebserviceInfoService.save(ywWebserviceInfo);
		} catch (Exception e) {
			logger.error("审批系统删除用户接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			System.out.println(e.getMessage());
		}
	}

	/**
	 * 方欣添加用户，调用新增保存
	 * 
	 * @param xmlStr
	 */
	public String newUser(String xmlStr) {
		try {
			Map<String,String> map = xmlToMap(xmlStr);		
			OmUserForm form = new OmUserForm(); 
			form.setUserCode(map.get("userCode"));
			form.setUserName(map.get("userName"));
			form.setPassword(map.get("encryptPassword"));
			form.setIsActive("Y".equals(map.get("status"))?"1":"0");
			System.out.println("map.get(status) = "+map.get("status"));
			System.out.println("Y.equals(map.get(status)) = "+"Y".equals(map.get("status")));
			System.out.println("N.equals(map.get(status)) = "+"N".equals(map.get("status")));
			form.setLoginName(map.get("userCode")); 
			form.setPosition(map.get("position"));
			form.setSex("1".equals(map.get("sex"))?"M":"F");
			System.out.println("map.get(sex) = "+map.get("sex"));
			System.out.println("1.equals(map.get(sex)) = "+"1".equals(map.get("sex")));
			System.out.println("0.equals(map.get(sex)) = "+"0".equals(map.get("sex")));
			form.setOfficeTel(map.get("telephone"));
			form.setMobile(map.get("mobilePhone"));
			form.setEmail(map.get("email"));
//			String createTimeStr = map.get("createTime");		
//			if(StringUtils.isNotBlank(createTimeStr)){
//				form.setCreateTime(DateUtils.stringToDate(createTimeStr,DateUtils.YYYY_MM_DD_HH_MM_SS));
//			}		
			String orgCode = map.get("orgCode");
			if(StringUtils.isNotBlank(orgCode)){
				omUserService.addUserToOrg(form, Long.parseLong(orgCode));
				return resultXml("0", "成功");
			}
		} catch (Exception e) {
			logger.error("方欣添加用户接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR+ e.getMessage(), e);
			System.out.println(e.getMessage());
			return resultXml("1", "添加用户失败");
		}
		return resultXml("1", "添加用户失败");
	}

	/**
	 * 方欣修改用户，调用修改保存
	 * 
	 * @param xmlStr
	 */
	public String modifyUser(String xmlStr) {
		try {
			Map<String,String> map = xmlToMap(xmlStr);	
			String userCode = map.get("userCode");
			OmUserForm form = omUserService.get(userCode);
			form.setUserCode(map.get("userCode"));
			form.setUserName(map.get("userName"));
			form.setPassword(map.get("encryptPassword"));
			form.setIsActive("Y".equals(map.get("states"))?"1":"0");
			form.setLoginName(map.get("userCode"));
			form.setPosition(map.get("position"));
			form.setSex("1".equals(map.get("sex"))?"M":"F");
			form.setOfficeTel(map.get("telephone"));
			form.setMobile(map.get("mobilePhone"));
			form.setEmail(map.get("email"));
//			String createTimeStr = map.get("createTime");		
//			if(StringUtils.isNotBlank(createTimeStr)){
//				form.setCreateTime(DateUtils.stringToDate(createTimeStr,DateUtils.YYYY_MM_DD_HH_MM_SS));
//			}	
//			String updateTimeStr = map.get("updateTime");		
//			if(StringUtils.isNotBlank(updateTimeStr)){
//				form.setUpdateTime(DateUtils.stringToDate(updateTimeStr,DateUtils.YYYY_MM_DD_HH_MM_SS));
//			}
//			String orgCode = map.get("orgCode");
			omUserService.save(form);
			return resultXml("0", "成功");
		} catch (Exception e) {
			logger.error("方欣修改用户接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR+ e.getMessage(), e);
			System.out.println(e.getMessage());
			return resultXml("1", "修改用户信息失败");
		}
	}
	
	/**
	 * 方欣修改用户密码，调用修改保存
	 * 
	 * @param xmlStr
	 */
	public String modifyUserPassword(String xmlStr) {
		try {
			Map<String,String> map = xmlToMap(xmlStr);
			String userCode = map.get("userCode");
			String newPassword = map.get("newPassword");
			OmUserForm form= omUserService.get(userCode);
			form.setPassword(newPassword);
			omUserService.save(form);
			return resultXml("0", "成功");
		} catch (Exception e) {
			logger.error("方欣修改用户密码接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR+ e.getMessage(), e);
			System.out.println(e.getMessage());
			return resultXml("1", "修改用户密码失败");
		}
	}
	
	/**
	 * 方欣删除用户，调用删除
	 * 
	 * @param xmlStr
	 */
	public String disuseUser(String xmlStr) {
		Map<String,String> map = xmlToMap(xmlStr);		
		String loginName = map.get("userCode");
		try {			
			if(StringUtils.isNotBlank(loginName)){
				OmUserForm form = omUserService.get(loginName);
				Long userId = form.getUserId();//查出userId
				Long orgId = getOrgIdByUserId(userId);//查出orgId
				System.out.println("userId = "+userId);
				System.out.println("orgId = "+orgId);
				if(orgId != null){
					omUserService.deleteUserFromOrg(userId, orgId,false);
				}				
				return resultXml("0", "成功");
			}
		} catch (Exception e) {
			logger.error("方欣删除用户接口保存异常" + SysTraceLogWriter.MESSAGE_SEPARATOR+ e.getMessage(), e);
			System.out.println(e.getMessage());
			return resultXml("1", "删除用户失败");
		}
		return resultXml("1", "删除用户失败");
	}

	/**
	 * xml字符串转换成map
	 * @param xmlStr
	 * @return
	 */
	public Map<String,String> xmlToMap(String xmlStr){
		/*String xmlStr = "<info><userCode>登录帐号</userCode><orgCode>组织编号</orgCode><userName>用户中文名</userName><encryptPassword>用户登录密码</encryptPassword>"+
						"<position>职务</position><sex>性别</sex><telephone>电话</telephone><mobilePhone>手机号码</mobilePhone><email>电子邮件</email>"+
						"<status>用户状态</status><createTime>创建时间</createTime></info>";
*/
		System.out.println("原始 = "+xmlStr);
		Map<String,String> map = new HashMap<String,String>();
		try {
			xmlStr = StringUtilsEX.base64Decode(xmlStr,"UTF-8");
			System.out.println("xmlStr = "+xmlStr);
			// 创建一个新的字符串		
			StringReader read = new StringReader(xmlStr);
			// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
			InputSource source = new InputSource(read);
			// 创建一个新的SAXBuilder
			SAXBuilder sb = new SAXBuilder();		
			// 通过输入源构造一个Document
			org.jdom.Document doc = sb.build(source);
			// 取的根元素
			org.jdom.Element root = doc.getRootElement();
//			System.out.println("根 = " + root.getName());
			// 得到根元素所有子元素的集合
			// org.jdom.Element eInfo = root.getChild("note");
			List jiedian = root.getChildren();
//			System.out.println("jiedian.size() = " + jiedian.size());
			org.jdom.Element et = null;
			for (int i = 0; i < jiedian.size(); i++) {
				et = (org.jdom.Element) jiedian.get(i);
//				System.out.println("et.getName() = " + et.getName() + "   et.getValue() = " + et.getValue());
				map.put(et.getName(), et.getValue());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return map;	
	}
	
	/**
	 * 构造内门户网站返回xml
	 * @param retCode
	 * @param message
	 * @return
	 */
	public String resultXml(String retCode,String message){
		String resultXml = "";
		try {
			Document document=DocumentHelper.createDocument();
			Element root=document.addElement("result");
			Element responseStatus = root.addElement("ResponseStatus");
			responseStatus.addElement("retCode").addText(retCode);
			responseStatus.addElement("retMessage").addText(message);
			resultXml = document.asXML();
			resultXml = StringUtilsEX.base64Encode(resultXml,"UTF-8");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return resultXml;
	}
	
	/**
	 * 从OM_USER_ORG表，根据userId查orgId
	 * @param userId
	 * @return
	 */
	public Long getOrgIdByUserId(long userId){
		String sql = "select t.ORG_ID from OM_USER_ORG t where t.user_id='"+userId+"'";
		List list = smartDao.getResultList(sql);
		if(list != null && list.size()>0){			
			return ((BigDecimal)list.get(0)).longValue();
		}
		return null;
	}
}
